package com.dongke.service.Impl;

import com.dongke.mapper.Menu1Mapper;
import com.dongke.mapper.Role1Menu1Mapper;
import com.dongke.pojo.Menu1;
import com.dongke.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class MenuServiceImpl implements MenuService {


    @Autowired
    private Menu1Mapper menu1Mapper;
    @Autowired
    private Role1Menu1Mapper role1Menu1Mapper;

    @Override
    public List<Map<String, Object>> getMenu(Integer uid) {
        List<Menu1> menu1s = menu1Mapper.selectByUid(uid);
        List<Map<String, Object>> menuTree = getMenuTree(0, menu1s);
        return menuTree;
    }

    @Override
    public boolean deleteMenuInfo(Integer id) {
        int i = menu1Mapper.deleteByPrimaryKey(id);
        return i>0;
    }

    @Override
    public List<Map<String, Object>> getZtreeCheked(Integer rid) {
        List<Menu1> newsList = menu1Mapper.selectByExample(null);
        List<Integer> news = role1Menu1Mapper.selectByRidByRid(rid);
        return getMenuZTreeToUpdate(newsList,0,news);
    }

    //递归遍历
    public List<Map<String,Object>> getMenuZTreeToUpdate(List<Menu1> list,Integer pid,List<Integer> mids){
        //定义一个集合进行数据的接收
        List<Map<String,Object>> mapList = new ArrayList<>();
        //遍历数据
        for (int i = 0; i < list.size(); i++) {
            Map<String,Object> map = null;
            //循环拿到条数据给当前实体类对象
            Menu1 menu = list.get(i);
            //进行判断  如果pid==实体类遍历出来的pid
            if (pid == menu.getPid()){

                map = new HashMap<>();

                //给当前map增加key和value
                map.put("id",menu.getId());
                map.put("name",menu.getText());
                if (mids != null && mids.size() > 0 && mids.contains(menu.getId())){
                    map.put("checked",true);
                }
                //参数  当前对象的id当做父级id找到子节点进行拼接
                map.put("children",getMenuZTreeToUpdate(list,menu.getId(),mids));

            }
            //判断map是否为空  不为空进入
            if (map != null){
                //给list集合追加数据
                mapList.add(map);
                //把nodes放到list集合中进行判断集合大小
                List<Map<String,Object>> maps = (List<Map<String,Object>>)map.get("children");
                //如果当前list集合小于等于0进入
                if (maps.size() <= 0 ){
                    //清除当前children
                    map.remove("children");
                }
            }
        }
        return mapList;
    }

    @Override
    public boolean updateMenuInfo(Menu1 menu) {
        int i = menu1Mapper.updateByPrimaryKeySelective(menu);
        return i>0;
    }

    @Override
    public Menu1 getInfo(Integer id) {
        Menu1 menu1 = menu1Mapper.selectByPrimaryKey(id);
        return menu1;
    }

    @Override
    public boolean addInfo(Menu1 menu1) {
        menu1Mapper.insertSelective(menu1);
        return false;
    }

    @Override
    public List<Map<String, Object>> getMenuZtree() {
        List<Menu1> menulist = menu1Mapper.selectByExample(null);
        List<Map<String, Object>> menuTree = getMenusTree(0, menulist);
        return menuTree;
    }

    //遍历菜单信息
    public List<Map<String, Object>> getMenusTree(Integer id, List<Menu1> menulist){
        //准备容器，存储遍历出来的数据
        List<Map<String, Object>> list = new ArrayList<>();
        //循环当前集合，进行对比
        for (int i = 0; i < menulist.size(); i++){
            //获取当前循环的元素对象
            Menu1 menu = menulist.get(i);
            //准备Map集合，拼接数据
            Map<String, Object> map = null;
            if (menu.getPid() == id){
                map = new HashMap<>();
                map.put("id", menu.getId());
                map.put("name", menu.getText());
                map.put("children", getMenusTree(menu.getId(), menulist));
            }
            if (map != null){
                list.add(map);
                List<Map<String, Object>> children = (List<Map<String, Object>>)map.get("children");
                if (children.size() <= 0){
                    map.remove("children");
                }
            }
        }
        return list;
    }

    private List<Map<String, Object>> getMenuTree(int i, List<Menu1> menu1s) {
        //准备容器放遍历出来的数据
        List<Map<String,Object>> objects = new ArrayList<>();
        //循环当前集合进行对比
        for (int a =0;a < menu1s.size();a++){
            Menu1 menu1 = menu1s.get(a);//获取到下标为0的menu
            Map<String,Object> map = null;//准备map容器拼接数据
            if(menu1.getPid() == i){
                map = new HashMap<>();
                map.put("id", menu1.getId());
                if(menu1.getUrl() == null || "".equals(menu1.getUrl())){
                    map.put("selectable", false);
                }
                map.put("text", menu1.getText());
                map.put("url", menu1.getUrl());
                map.put("nodes", getMenuTree(menu1.getId(), menu1s));
            }
            //如果没有链接则无法点击
            if (map != null){
                objects.add(map);
                //如果没有节点则不显示+，-号
                List<Map<String, Object>> nodes = (List<Map<String, Object>>)map.get("nodes");
                if (nodes.size() <= 0){
                    map.remove("nodes");
                }
            }
        }
        return objects;
    }
}
